package com.itzz.spring_zhoneHeng.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itzz.spring_zhoneHeng.base.RestResponse;
import com.itzz.spring_zhoneHeng.dao.BookDao;
import com.itzz.spring_zhoneHeng.dao.BookShelfDao;
import com.itzz.spring_zhoneHeng.dao.OrderDao;
import com.itzz.spring_zhoneHeng.dao.UserDao;
import com.itzz.spring_zhoneHeng.pojo.Book;
import com.itzz.spring_zhoneHeng.pojo.BookShelf;
import com.itzz.spring_zhoneHeng.pojo.Order;
import com.itzz.spring_zhoneHeng.pojo.User;
import com.itzz.spring_zhoneHeng.service.BookShelfService;
import com.itzz.spring_zhoneHeng.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    OrderDao dao;

    @Autowired
    BookDao bookDao;

    @Autowired
    BookShelfDao bookShelfDao;

    @Autowired
    BookShelfService bookShelfService;

    @Autowired
    UserDao userDao;

    //添加订单
    @Override
    @Transactional
    public RestResponse addOrder(Order order,Integer bookId,Integer userId) {
//        if(order!=null){ }
            Date date = new Date();
//            SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
//            formatter.format(date);
            order.setDate(date);
            order.setId(0);
            //为未支付状态
            order.setState(1);
            //设定死money为20
            order.setMoney(20.0);

        //(1)添加订单 (返回添加订单的id)
        dao.addOrder(order);
        //(2)返回classification 的id    (通过bookid查询分类id返回分类id)
        int classid = bookDao.queryClassId(bookId);
        //添加classid  到 o_c表中  (最后添加到o_c表中)
        dao.addO_C(order.getId(),classid);
        //添加o_b表中
        dao.addO_B(order.getId(),bookId);
        //最后添加到 用户和订单表u_o中
        Integer i = dao.addU_O(userId, order.getId());
//        添加到分页插件中
        if(i>0){
           return RestResponse.ok();
        }else{
            return RestResponse.fail(500,"添加失败");
        }
    }


    //查询所有的订单
    @Override
    public RestResponse queryAllOrder(Order order, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
//        yyyy-dd-MM
//        SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
        List<Order> orders = dao.queryAllOrder(order);
//        for(Order o:orders){
//            o.setDate(formatter.format(o.getDate()));
//        }
        PageInfo<Order> pageInfo = new PageInfo<>(orders);
        return RestResponse.ok(pageInfo);
    }


    //根据订单id删除订单
    @Override
    @Transactional
    public RestResponse delOrder(Integer id) {
        //(2)删除对应的O_C关系表
        dao.delO_C(id);
        //(3)删除对应的O_B关系表   根据oid
        dao.delO_B(id);
        //(4)删除对应的u_o关系表
        Integer integer = dao.delU_O(id);
        //(1)删除订单
        int i = dao.delOrder(id);


        if(integer>0){
            return RestResponse.ok(i);
        }
        return RestResponse.fail(500,"删除失败");
    }


    //根据订单id修改订单的状态
    @Override
    @Transactional
    public RestResponse updateOrder(HashMap<String,Object> map, Integer id ) {
        if(id==null){
            return RestResponse.fail(500,"订单不存在");
        }
        // (就是说修改订单的状态  需要判断余额是否足够)  不够就跳转到充值
//        bookShelfService.updateBook(id,1);
        User user = new User();
        user.setUsername((String) map.get("username"));
        int money= (int) map.get("myMoney");
        user.setMoney(Double.valueOf(money));

        //修改用户的余额
        userDao.updateMoney(user.getUsername(),user.getMoney());
        //根据orderId  查询到对应的O_B表中的bookid
        Integer bookId = dao.queryBookId(id);
        //根据bookid 添加到书架
        BookShelf bookShelf = new BookShelf();
        bookShelf.setPath((String) map.get("path"));
        bookShelf.setBookName((String) map.get("bookName"));
        bookShelf.setAuthor((String) map.get("author"));
        //简介  根据bookId  查询这本书的简介
        String introduce = bookDao.selectIntroduce(bookId);
        bookShelf.setIntroduce(introduce);
        bookShelf.setCategory((String) map.get("category"));
        bookShelf.setVip(2);

        Integer userId = (Integer) map.get("userId");

        //添加书架
        RestResponse response = bookShelfService.addBook(bookShelf, bookId, 1,userId);
        //根据订单id修改订单的支付状态  如果足够  则修改订单的状态
        int i = dao.updateOrderState(id, 2);


        return response;
    }

}
